package com.yanfan.dlt698_45.message;

import com.yanfan.dlt698_45.DLT698Utils;
import com.yanfan.dlt698_45.frame.DLT698_45Frame;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

/**
 * @author oliver foo
 * @description
 * @date 2023/3/21 9:05
 */
public class DLT698_45Message {
    protected final DLT698_45Frame frame;


    /**
     * 前导唤醒符 0xFE 的个数
     */
    protected final int wakeCodeNum;


    public DLT698_45Message(DLT698_45Frame frame, int wakeCodeNum) {
        this.frame = frame;
        this.wakeCodeNum = wakeCodeNum;
    }


    public int getWakeCodeNum() {
        return wakeCodeNum;
    }

    public DLT698_45Frame getFrame() {
        return frame;
    }


    public void getMessageData(ByteBuf buf) {
        ByteBuf data = Unpooled.buffer();
        frame.write(data);
        short fcs = DLT698Utils.computeFcs16(data);
        for (int i = 0; i < wakeCodeNum; i++) {
            buf.writeByte(DLT698Utils.DEFAULT_DLT698_WAKE_CODE);
        }
        buf.writeByte(DLT698Utils.DEFAULT_DLT698_START_ID);
        buf.writeBytes(data);
        buf.writeShort(fcs);
        buf.writeByte(DLT698Utils.DEFAULT_DLT698_END_ID);
    }
}
